<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width=" '110px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                >
                    <el-row>
                        <el-col :span="10">
                            <FormItem
                                :label="t('visit.CORPID')"
                                v-model="baTable.form.items!.CORPID"
                                prop="CORPID"
                                type="remoteSelect"
                                :input-attr="{
                                    pk: 'CORP',
                                    field: 'CORP',
                                    'remote-url': corplib + 'index',
                                    placeholder: t('Click Select'),
                                    onChange:getCurrentCorp
                                }"
                            />
                            <FormItem
                                :label="t('visit.CORPNAME')"
                                type="string"
                                v-model="baTable.form.items!.CORPNAME"
                                prop="CORPNAME"
                                :input-attr="{ placeholder: t('', { field: t('') }),disabled:true }"
                            />
                            <FormItem
                                :label="t('visit.ENAME')"
                                type="string"
                                v-model="baTable.form.items!.ENAME"
                                prop="ENAME"
                                :input-attr="{ placeholder: t('', { field: t('') }),disabled:true }"
                            />
                            <FormItem
                                :label="t('visit.ADDRESS')"
                                type="string"
                                v-model="baTable.form.items!.ADDRESS"
                                prop="ADDRESS"
                                :input-attr="{ placeholder: t('', { field: t('') }), disabled:true}"
                            />
                            <FormItem
                                :label="t('visit.SALER')"
                                type="string"
                                v-model="baTable.form.items!.SALER"
                                prop="SALER"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.SALER') }) }"
                            />
                            <FormItem
                                :label="t('visit.BACKGROUND')"
                                type="textarea"
                                v-model="baTable.form.items!.BACKGROUND"
                                prop="BACKGROUND"
                                :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.BACKGROUND') }) }"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            />
                            <FormItem
                                :label="t('visit.UPCOMING')"
                                type="textarea"
                                v-model="baTable.form.items!.UPCOMING"
                                prop="UPCOMING"
                                :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.UPCOMING') }) }"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            />
                        </el-col>
                        <el-col :span="14" class="info-wrapper">
                            <FormItem
                                :label="t('visit.CORPMAN')"
                                type="string"
                                v-model="baTable.form.items!.CORPMAN"
                                prop="CORPMAN"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.CORPMAN') }) }"
                            />
                            <FormItem
                                :label="t('visit.POSITION')"
                                type="string"
                                v-model="baTable.form.items!.POSITION"
                                prop="POSITION"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.POSITION') }) }"
                            />
                            <FormItem
                                :label="t('visit.TELEPHONE')"
                                type="string"
                                v-model="baTable.form.items!.TELEPHONE"
                                prop="TELEPHONE"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.TELEPHONE') }) }"
                            />
                            <FormItem
                                :label="t('visit.FAX')"
                                type="string"
                                v-model="baTable.form.items!.FAX"
                                prop="FAX"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.FAX') }) }"
                            />
                            <FormItem
                                :label="t('visit.EMAIL')"
                                type="string"
                                v-model="baTable.form.items!.EMAIL"
                                prop="EMAIL"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.EMAIL') }) }"
                            />
                            <FormItem
                                :label="t('visit.OP')"
                                type="string"
                                v-model="baTable.form.items!.OP"
                                prop="OP"
                                :input-attr="{ placeholder: '当前登录用户', disabled: true }"
                            />
                            <FormItem
                                :label="t('visit.POST')"
                                type="string"
                                v-model="baTable.form.items!.POST"
                                prop="POST"
                                :input-attr="{ placeholder: t('Please input field', { field: t('visit.POST') }) }"
                            />
                            <FormItem
                                :label="t('visit.OPTIME')"
                                type="date"
                                v-model="baTable.form.items!.OPTIME"
                                prop="OPTIME"
                                :input-attr="{ placeholder: '当前日期', disabled: true }"
                            />
                            
                            <FormItem
                                :label="t('visit.VISITDT')"
                                type="datetime"
                                v-model="baTable.form.items!.VISITDT"
                                prop="VISITDT"
                                :input-attr="{ placeholder: t('Please select field', { field: t('visit.VISITDT') }) }"
                            />
                            <FormItem
                                :label="t('visit.SHAREFLAG')"
                                type="select"
                                v-model="baTable.form.items!.SHAREFLAG"
                                :data="{ content: { '0': '个人查看', '1': '本部门共享' } }"
                            />
                            <FormItem
                                style="width: 100%"
                                :label="t('visit.RELATIONSHIP')"
                                type="textarea"
                                v-model="baTable.form.items!.RELATIONSHIP"
                                prop="RELATIONSHIP"
                                :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.RELATIONSHIP') }) }"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            />
                            <FormItem
                                style="width: 100%"
                                :label="t('visit.PURPOSE')"
                                type="textarea"
                                v-model="baTable.form.items!.PURPOSE"
                                prop="PURPOSE"
                                :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.PURPOSE') }) }"
                                @keyup.enter.stop=""
                                @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            />
                        </el-col>
                    </el-row>
                    <FormItem
                        :label="t('visit.DETAIL')"
                        type="textarea"
                        v-model="baTable.form.items!.DETAIL"
                        prop="DETAIL"
                        :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.DETAIL') }) }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                    />

                    
                    
                    
                    <div style="display:flex">
                        <FormItem
                            :label="t('visit.NEXTVISITDT')"
                            type="datetime"
                            v-model="baTable.form.items!.NEXTVISITDT"
                            prop="NEXTVISITDT"
                            :input-attr="{ placeholder: t('Please select field', { field: t('visit.NEXTVISITDT') }) }"
                        />
                        <FormItem
                            :label="t('提醒時間')"
                            type="datetime"
                            v-model="baTable.form.items!.XPERTIME"
                            prop="XPERTIME"
                            :input-attr="{ placeholder: t('Please select field', { field: t('提醒時間') }) }"
                        />
                        <FormItem :label="t('生成个人事务提醒')" :label-width="150+'px'" type="switch" v-model="baTable.form.items!.XPER" prop="" />
                    </div>
                    <FormItem
                        :label="t('visit.NEXTDETAIL')"
                        type="textarea"
                        v-model="baTable.form.items!.NEXTDETAIL"
                        prop="NEXTDETAIL"
                        :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('visit.NEXTDETAIL') }) }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                    />
                    <FormItem
                        style="width: 100%"
                        :label="t('visit.UPLOADFILE')"
                        type="file"
                        v-model="baTable.form.items!.UPLOADFILE"
                        prop="UPLOADFILE"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { corplib, codelib } from '/@/api/controllerUrls'
import { getSelectData } from '/@/api/common'
const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    CORPID: [buildValidatorData({ name: 'required', title: t('visit.CORPID') })],
    SALER: [buildValidatorData({ name: 'required', title: t('visit.SALER') })],
    VISITDT: [buildValidatorData({ name: 'required', message: t('Please enter the correct field', { field: t('visit.VISITDT') }) })],
    OPTIME: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('visit.OPTIME') }) })],
    NEXTVISITDT: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('visit.NEXTVISITDT') }) })],
})

const getCurrentCorp = (name:string)=>{
    let url = corplib + 'index'
    getSelectData(url,name,{
        initValue:name
    }).then(res=>{
        let item = res.data.list[0] || {}
        baTable.form.items!.CORPNAME = item.CORPNAME
        baTable.form.items!.ENAME = item.ENAME
        baTable.form.items!.ADDRESS = item.ADDRESS

    })
}


</script>

<style scoped lang="scss">
.info-wrapper {
    display: flex;
    flex-wrap: wrap;
    > div {
        width: 50%;
    }
}
</style>
